//
//  DDRetrievePwdViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/14.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDRetrievePwdViewController.h"
#import "JJRGetCodeButton.h"
#import "DDLoginManager.h"

@interface DDRetrievePwdViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet UIView *retrieveBackView;
@property (weak, nonatomic) IBOutlet UITextField *emailTxf;
@property (weak, nonatomic) IBOutlet UITextField *verifyTxf;
@property (weak, nonatomic) IBOutlet UITextField *npwdTxf;
@property (weak, nonatomic) IBOutlet UITextField *confirmTxf;
@property (weak, nonatomic) IBOutlet JJRGetCodeButton *verifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (strong, nonatomic) DDLoginManager *manager;
@end

@implementation DDRetrievePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDLoginManager new];
    [self layoutRetrieveView];
}
- (void)layoutRetrieveView
{
    self.retrieveBackView.layer.cornerRadius = 3.0;
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = ScreenHeight-64;
    self.confirmBtn.layer.cornerRadius = 3.0;
    self.verifyBtn.layer.cornerRadius = 3.0;
}
- (IBAction)getVerifyCodeClick:(id)sender
{
    if (![NOValidateHelper validateEmail:self.emailTxf.text]) {
        [self showMessageHUD:@"Please input valid email!" delayHide:1.0];
        return;
    }
    
    [self.verifyBtn getCodeBtnFinished:^{
        
    }];
    [self.verifyBtn fireCodeTimer];
    
    [[self.manager retrievePasswordVerifyCode:self.emailTxf.text]subscribeNext:^(id x) {
        
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
    
}
- (BOOL)checkInput
{
    if (![NOValidateHelper validateEmail:self.emailTxf.text]) {
        [self showMessageHUD:@"Please enter a valid email" delayHide:1.0];
        return NO;
    }
    if (self.verifyTxf.text.length == 0) {
        [self showMessageHUD:@"Please enter verify code" delayHide:1.0];
        return NO;
    }
    if (![NOValidateHelper validatePassword:self.npwdTxf.text]) {
        [self showMessageHUD:@"Please enter a valid password" delayHide:1.0];
        return NO;
    }
    if (![self.confirmTxf.text isEqualToString:self.npwdTxf.text]) {
        [self showMessageHUD:@"Please confirm the new password" delayHide:1.0];
        return NO;
    }
    return YES;
}
- (IBAction)confirmClick:(id)sender
{
    if (![self checkInput]) {
        return;
    }
    weak_Self(self);
    [[self.manager retrievePassword:self.emailTxf.text code:self.verifyTxf.text pwd:self.npwdTxf.text confirmPwd:self.confirmTxf.text]subscribeNext:^(id x) {
        [weakSelf showMessageHUD:@"Retrieve password success" delayHide:1.0];
        [weakSelf gotoBack];
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
